﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Elkitools.EditorParts.ListSelector.Tools;

namespace Elkitools.EditorParts.ListSelector.Controls
{
    [Serializable]
    public partial class CalendarsWebsAndLists : UserControl
    {
        protected TreeView treeCalendarsWebsAndLists;
        protected Panel pnlHost;

        /// <summary>
        /// Concatened string representing all checked lists (join = ";")
        /// </summary>
        public string ListsUrls
        {
            get
            {
                string result = string.Empty;
                //------------------------------------------------------

                try
                {
                    result = ViewModel.GetCheckedListsUrl(treeCalendarsWebsAndLists);
                }
                catch (Exception excListsUrls)
                {
                    UlsLogger.Error("An error occurred while trying to GET all checked lists urls in the editorpart treeview : " + excListsUrls.Message);
                }

                //------------------------------------------------------
                return result;
            }

            set
            {
                try
                {
                    ViewModel.SetCheckedListsUrl(treeCalendarsWebsAndLists, value);
                }
                catch (Exception excListsUrls)
                {
                    UlsLogger.Error("An error occurred while trying to SET checked lists urls in the editorpart treeview : " + excListsUrls.Message);
                }
            }
        }

        private CalendarViewModel _viewModel = null;
        /// <summary>
        /// ViewModel of the TreeView Component
        /// </summary>
        internal CalendarViewModel ViewModel
        {
            get
            {
                if (_viewModel == null)
                {
                    // We first need to ensure controls are ready
                    EnsureChildControls();

                    // We then initialize the ViewModel 
                    InitializeViewModel();
                }

                return _viewModel;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// Initializes presentation and controls
        /// </summary>
        private void InitializeView()
        {
        }

        /// <summary>
        /// Initializes Data and Logic
        /// </summary>
        private void InitializeViewModel()
        {
            try
            {
                _viewModel = new CalendarViewModel(SPContext.Current.Site);
            }
            catch (Exception excInitViewModel)
            {
                UlsLogger.Error("An error occurred while trying to initialize CalendarViewModel from CalendarsWebsAndLists : " + excInitViewModel.Message);
                throw;
            }
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            // Add the hosting Panel
            pnlHost = new Panel();
            pnlHost.ID = this.ID + "_PanelHost";

            // Add the TreeView
            treeCalendarsWebsAndLists = new TreeView();
            treeCalendarsWebsAndLists.ID = this.ID + "_TreeCalendarsWebsAndLists";

            pnlHost.Controls.Add(treeCalendarsWebsAndLists);


            // Fill the TreeView
            if (treeCalendarsWebsAndLists != null && treeCalendarsWebsAndLists.Nodes.Count == 0)
            {
                ViewModel.FillTree(treeCalendarsWebsAndLists);
            }

            this.Controls.Add(pnlHost);
        }
    }
}